package com.holdtime.jtt809dataservice.business.hd.impl;

import com.holdtime.common.utils.general.ApiResponseModel;
import com.holdtime.jtt809.taxi.module.entity.PlatformInfoDO;
import com.holdtime.jtt809.taxi.module.entity.PlatformLoginLogDO;
import com.holdtime.jtt809.taxi.module.entity.PlatformLogoutLogDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformInfoMapper;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformLoginLogMapper;
import com.holdtime.jtt809.taxi.module.entity.mapper.PlatformLogoutLogMapper;
import com.holdtime.jtt809dataservice.business.api.LinkService;
import com.holdtime.jtt809dataservice.common.Jtt809ErrorEnum;
import com.holdtime.jtt809dataservice.common.bean.MainLinkLogOutReqDTO;
import com.holdtime.jtt809dataservice.common.bean.MainLinkLoginReqDTO;
import com.holdtime.jtt809dataservice.common.bean.MainLinkLoginResDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/***
 *
 */
@Slf4j
@Service
public class LinkServiceImpl implements LinkService {
    @Resource
    private PlatformInfoMapper platformInfoMapper;
    @Resource
    private PlatformLoginLogMapper platformLoginLogMapper;
    @Resource
    private PlatformLogoutLogMapper platformLogoutLogMapper;

    @Override
    public ApiResponseModel<MainLinkLoginResDTO> dealMainLinkLoginReq(MainLinkLoginReqDTO mainLinkLoginReqDTO) {
        ApiResponseModel<MainLinkLoginResDTO> model = new ApiResponseModel<>();
        MainLinkLoginResDTO mainLinkLoginResDTO = new MainLinkLoginResDTO();
        PlatformInfoDO platformInfoDO = platformInfoMapper.selectOne(PlatformInfoDO::getUserid, mainLinkLoginReqDTO.getUserid());
        PlatformLoginLogDO platformLoginLogDO = new PlatformLoginLogDO();
        platformLoginLogDO.setUserid(mainLinkLoginReqDTO.getUserid());
        platformLoginLogDO.setDlsj(LocalDateTime.now());
        platformLoginLogDO.setPfGnsscenterid(mainLinkLoginReqDTO.getJrm());
        platformLoginLogDO.setLinkIp(mainLinkLoginReqDTO.getIp());
        if (null == platformInfoDO){
            mainLinkLoginResDTO.setResult(Jtt809ErrorEnum.USER_NOT_REGISTER.getCode());
            model.error(Jtt809ErrorEnum.USER_NOT_REGISTER.getMessage());
        }else if (!platformInfoDO.getPassword().equals(mainLinkLoginReqDTO.getPassword())){
            mainLinkLoginResDTO.setResult(Jtt809ErrorEnum.PASSWORD_ERROR.getCode());
            model.error(Jtt809ErrorEnum.PASSWORD_ERROR.getMessage());
        }else if (!platformInfoDO.getLinkIp().contains(mainLinkLoginReqDTO.getIp())){
            mainLinkLoginResDTO.setResult(Jtt809ErrorEnum.IP_ERROR.getCode());
            model.error(Jtt809ErrorEnum.IP_ERROR.getMessage());
        }else if (!platformInfoDO.getPfGnsscenterid().equals(mainLinkLoginReqDTO.getJrm())){
            mainLinkLoginResDTO.setResult(Jtt809ErrorEnum.JRM_ERROR.getCode());
            model.error(Jtt809ErrorEnum.JRM_ERROR.getMessage());
        }else{
            mainLinkLoginResDTO.setResult(Jtt809ErrorEnum.SUCCESS.getCode());
        }
        // 新增登录记录
        platformLoginLogDO.setResult(mainLinkLoginResDTO.getResult());
        platformLoginLogMapper.insert(platformLoginLogDO);
        model.setRecord(mainLinkLoginResDTO);
        if (mainLinkLoginResDTO.getResult() != Jtt809ErrorEnum.SUCCESS.getCode()){
            return model;
        }
        return model.success();
    }

    @Override
    public ApiResponseModel<?> dealMainLinkLogOutReq(MainLinkLogOutReqDTO mainLinkLogOutReqDTO) {
        ApiResponseModel model = new ApiResponseModel<>();
        PlatformInfoDO platformInfoDO = platformInfoMapper.selectOne(PlatformInfoDO::getUserid, mainLinkLogOutReqDTO.getUseId());
        PlatformLogoutLogDO platformLogoutLogDO = new PlatformLogoutLogDO();
        platformLogoutLogDO.setUserid(mainLinkLogOutReqDTO.getUseId());
        platformLogoutLogDO.setDcsj(LocalDateTime.now());
        platformLogoutLogDO.setLinkIp(mainLinkLogOutReqDTO.getIp());
        if (null == platformInfoDO){
            model.error(Jtt809ErrorEnum.USER_NOT_REGISTER.getMessage());
        }else if (!platformInfoDO.getPassword().equals(mainLinkLogOutReqDTO.getPassword())){
            model.error(Jtt809ErrorEnum.PASSWORD_ERROR.getMessage());
        }else if (!platformInfoDO.getLinkIp().contains(mainLinkLogOutReqDTO.getIp())){
            model.error(Jtt809ErrorEnum.IP_ERROR.getMessage());
        }else{
            return model.success();
        }
        // 新增登出记录
        platformLogoutLogDO.setCreator(model.getMsg());
        platformLogoutLogMapper.insert(platformLogoutLogDO);
        return model;
    }
}
